﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;

namespace CommunitySystem.PageView
{
    public class LinShi
    {
        private int _ID;
        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        private int _OrgID;
        public int OrgID
        {
            get { return _OrgID; }
            set { _OrgID = value; }
        }
        private string _JiGouMingCheng;
        public string JiGouMingCheng
        {
            get { return _JiGouMingCheng; }
            set { _JiGouMingCheng = value; }
        }
        private string _JiGouMingChengS;
        public string JiGouMingChengS
        {
            get { return _JiGouMingChengS; }
            set { _JiGouMingChengS = value; }
        }
        private string _CanUse;
        public string CanUse
        {
            get { return _CanUse; }
            set { _CanUse = value; }
        }
        private string _Demo;
        public string Demo
        {
            get { return _Demo; }
            set { _Demo = value; }
        }
        private bool _IsSelected;
        public bool IsSelected
        {
            get { return _IsSelected; }
            set { _IsSelected = value; }
        }
    }
    public partial class BianZhiDetailWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        public ObservableCollection<EmpbasicService.BianZhiModel> BianZhiModels =
            new ObservableCollection<EmpbasicService.BianZhiModel>();

        private List<LinShi> list = new List<LinShi>();
        public LinShi SelectItems;
        public int KeShiID;
        public string Type = "";
        public BianZhiDetailWindow(string orgid, string type, int keshiid)
        {
            InitializeComponent();
            KeShiID = keshiid;
            Type = type;
            Client.GetBianZhiDetail_JieYongModelCompleted += new EventHandler<EmpbasicService.GetBianZhiDetail_JieYongModelCompletedEventArgs>(Client_GetBianZhiDetail_JieYongModelCompleted);
            Client.GetBianZhiDetail_JieYongModelAsync(orgid, type);
            Client.UpdateBianZhiDetail_JieYongModelCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_UpdateBianZhiDetail_JieYongModelCompleted);
        }
        void Client_GetBianZhiDetail_JieYongModelCompleted(object sender, EmpbasicService.GetBianZhiDetail_JieYongModelCompletedEventArgs e)
        {
            list.Clear();
            foreach (BianZhiModel i in e.Result)
            {
                LinShi s = new LinShi { ID = i.ID, JiGouMingChengS = i.JiGouMingChengS, CanUse = i.CanUse, OrgID = i.OrgID, Demo = i.Demo, JiGouMingCheng = i.JiGouMingCheng };
                list.Add(s);
            }
            lb.ItemsSource = null;
            lb.ItemsSource = list;
        }


        public BianZhiDetailWindow(ObservableCollection<BianZhiModel> zhengkemodels, ObservableCollection<BianZhiModel> fukemodels, ObservableCollection<BianZhiModel> zhurenmodels,
            ObservableCollection<BianZhiModel> fuzhurenmodels, string type, int keshiid)
        {
            InitializeComponent();
            list.Clear();
            Type = type;
            KeShiID = keshiid;
            if (zhengkemodels != null)
            foreach (BianZhiModel i in zhengkemodels)
            {
                i.CanUse = i.ZhengKeShiJi;
                i.JiGouMingChengS = i.JiGouMingCheng + "-正科职数";
                LinShi s = new LinShi {ID = i.ID, JiGouMingChengS = i.JiGouMingChengS, CanUse = i.CanUse,OrgID = i.OrgID,Demo = i.Demo,JiGouMingCheng = i.JiGouMingCheng};
                list.Add(s);
            }
            if (fukemodels != null)
            foreach (BianZhiModel i in fukemodels)
            {
                i.CanUse = i.FuKeShiJi;
                i.JiGouMingChengS = i.JiGouMingCheng + "-副科职数";
                LinShi s = new LinShi { ID = i.ID, JiGouMingChengS = i.JiGouMingChengS, CanUse = i.CanUse, OrgID = i.OrgID, Demo = i.Demo, JiGouMingCheng = i.JiGouMingCheng };
                list.Add(s);
            }
            if (zhurenmodels != null)
            foreach (BianZhiModel i in zhurenmodels)
            {
                i.CanUse = i.ZhuRenShiJi;
                i.JiGouMingChengS = i.JiGouMingCheng + "-主任职数";
                LinShi s = new LinShi { ID = i.ID, JiGouMingChengS = i.JiGouMingChengS, CanUse = i.CanUse, OrgID = i.OrgID, Demo = i.Demo, JiGouMingCheng = i.JiGouMingCheng };
                list.Add(s);
            }
            if (fuzhurenmodels != null)
            foreach (BianZhiModel i in fuzhurenmodels)
            {
                i.CanUse = i.FuZhuRenShiJi;
                i.JiGouMingChengS = i.JiGouMingCheng + "-副主任职数";
                LinShi s = new LinShi { ID = i.ID, JiGouMingChengS = i.JiGouMingChengS, CanUse = i.CanUse, OrgID = i.OrgID, Demo = i.Demo, JiGouMingCheng = i.JiGouMingCheng };
                list.Add(s);
            }

            lb.ItemsSource = null;
            lb.ItemsSource = list;
            Client.UpdateBianZhiDetail_JieYongModelCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_UpdateBianZhiDetail_JieYongModelCompleted);
        }

        void lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
          
            SelectItems = lb.SelectedItem as LinShi;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            SelectItems = null;
            var source = lb.ItemsSource as List<LinShi>;
            if (source != null && source.Count > 0) SelectItems = source.Where(c => c.IsSelected == true).FirstOrDefault();
            //SelectItems = lb.SelectedItem as LinShi;
            if (SelectItems != null)
            {
                BianZhiModel b = new BianZhiModel
                {
                    ID = SelectItems.ID,
                    JiGouMingChengS = SelectItems.JiGouMingChengS,
                    CanUse = SelectItems.CanUse,
                    OrgID = SelectItems.OrgID,
                    Demo = SelectItems.Demo,
                    JiGouMingCheng = SelectItems.JiGouMingCheng
                };
                if (MessageBox.Show("确定借用该科室编制吗？", "系统提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Client.UpdateBianZhiDetail_JieYongModelAsync(KeShiID.ToString(), Type, b, 0, "", false);
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要借用的科室！");
        }

        void Client_UpdateBianZhiDetail_JieYongModelCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("借用成功！");
            this.DialogResult = true;
        }

        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Match m = Regex.Match(((TextBox)sender).Text, pattern); // 匹配正则表达式
            if (!m.Success) // 输入的不是数字
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                ((TextBox)sender).Text = "";
                return;
            }
        }
    }
}
